/* $Header: dig_path.h,v 2.2 01/10/02 10:30:16 przybyls Exp $ */


/***********************************************************************/
/* INCLUDE FILE NAME: dig_path.h                                       */
/* ==================                                                  */
/*                                                                     */
/* Author:  Bob Cook                                                   */
/*                                                                     */
/* COMMENT:                                                            */
/*          Includes for dig_path.c.                                   */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

#ifndef DIG_PATH_H
#define DIG_PATH_H

/*****************************/
/*   IFDEFS                  */
/*****************************/


/* variables used for Digital Path Test */
extern  U_INT16     DigitalPathTestMode;
extern  POINT_TYPE  Test_8Byte[];
extern  U_INT8      DPT_Subtract[2];
extern  U_INT32      DPT_Shift_Num[2];



/*****************************/
/*   SYMBOLIC CONSTANTS      */
/*****************************/

#define SIGNAL1    0      /* signal channel 1 - used for referencing arrays */
#define SIGNAL2    1      /* signal channel 2 - used for referencing arrays */
#define SIGNALP    2      /* signal plot channel - used for referencing arrays */

#define NUM_SIGNALS  3    /* number of digital signals channels in instrument */

#define MARK_32_POINT   (INT16)0x8000   /* marks a 32 bit point in SigBuf */

/* Set buffer sizes */
#define SIGBUF1_SIZE  200000  /* 2 byt/wd * 200000 pts = 400000 bytes */
#define SIGBUF2_SIZE  200000  /* 2 byt/wd * 200000 pts = 400000 bytes */
#define SIGBUFP_SIZE   10000  /* 2 byt/wd *  10000 pts =  20000 bytes */

#define COMPRESS_FULL_POINT_MARKER  0x7FFF
#define COMPRESS_START_MARKER 0x7FFE
#define COMPRESS_STOP_MARKER 0x7FFD
#define COMPRESS_START_STOP_MARKER 0x7FFC

/* maximum number of compressed points sent before a full point is sent */
#define MAX_COMPRESS_COUNT  2000

/* for DigitalPathTesting */
#define  DPT_MAX_INCR  0xF4A54800

/*****************************/
/*   MACROS                  */
/*****************************/


/*==========================================================================*
 * MACRO: PutBuf                                                            *
 *                                                                          *
 * PURPOSE: put data into circular queue and update count                   *
 * WARNING: 1. Count variable is modified elsewhere.                        *
 *          2. Check for overflow before calling this macro.                *
 *==========================================================================*/

#define PutBuf(buf_v, item)                                                  \
/* void PutBuf(SIGBUF_TYPE buf_v, INT16 item) */                             \
/* void PutBuf(FRMTBUF_TYPE buf_v, INT32 item) */                            \
                                                                             \
{                                                                            \
                                                                             \
   /* Check if ptr is at end of buffer. If it is, move to beginning. */      \
   if (buf_v.prear == &buf_v.pbuffer[buf_v.size - 1])                        \
      buf_v.prear = buf_v.pbuffer;                                           \
   else                                                                      \
      buf_v.prear++;                                                         \
                                                                             \
   *buf_v.prear = (item);                                                    \
   buf_v.count++;                                                            \
}                                                                            \



/*==========================================================================*
 * MACRO: GetBuf                                                            *
 *                                                                          *
 * PURPOSE: remove data from circular queue and update count                *
 * WARNING: 1. Count variable is modified elsewhere.                        *
 *          2. Check for overflow before calling this macro.                *
 *==========================================================================*/

#define GetBuf(buf_v, item_addr)                                             \
/* void GetBuf(SIGBUF_TYPE buf_v, INT16 *item_addr) */                       \
/* void GetBuf(FRMTBUF_TYPE buf_v, INT32 *item_addr) */                      \
                                                                             \
{                                                                            \
                                                                             \
   /* Check if ptr is at end of buffer. If it is, move to beginning. */      \
   if (buf_v.pfront == &buf_v.pbuffer[buf_v.size - 1])                       \
      buf_v.pfront = buf_v.pbuffer;                                          \
   else                                                                      \
      buf_v.pfront++;                                                        \
                                                                             \
   *(item_addr) = *buf_v.pfront;                                             \
   buf_v.count--;                                                            \
}                                                                            \


/*****************************/
/*   TYPEDEFS                */
/*****************************/

/*
 * define data structure containing the variables used by the main data
 *    buffers - SigBuf1, SigBuf2 and SigBufP
 */
typedef  struct
{
   INT16  *pbuffer;        /* pointer to data buffer */
   INT16  *pfront;         /* front of queue - remove data from front */
   INT16  *prear;          /* rear of queue - add data to rear */
   U_INT32  size;          /* size of queue in long words */
   U_INT32  count;         /* number of long words in queue */
   INT32   last_in;        /* 32 bit value of last point put in queue */
   INT32   last_out;       /* 32 bit value of last point removed from queue */
} SIGBUF_TYPE;


/*
 * define data structure containing the variables used by a small buffer
 *    containing the formatted data which is ready to be placed into a
 *    data message for the host.
 *    One buffer for each channel: FrmtBuf1 and FrmtBuf2.
 */

typedef  struct
{
   INT32  *pbuffer;        /* pointer to data buffer */
   INT32  *pfront;         /* front of queue - remove data from front */
   INT32  *prear;          /* rear of queue - add data to rear */
   U_INT16  size;          /* size of queue in words */
   U_INT16  count;         /* number of 16 bit words in queue */
} FRMTBUF_TYPE;

typedef struct
{
   POINT_TYPE point;
   POINT_TYPE point_last;
   POINT_TYPE diff;
   POINT_TYPE diff_last;
   POINT_TYPE diff2;
} SIGNAL_CB_TYPE;



/*****************************/
/*   FUNCTION PROTOTYPES     */
/*****************************/

#ifndef mccabe
/* put 512 routine in short addressing section */
#pragma SECTION PROG=SysCode
#endif
void CompressAndStore(U_INT8 Signal,DIG48 *pDigital_Data,U_INT16 Data_Count);
#ifndef mccabe
#pragma SECTION UNDO
#endif

void Add64(POINT_TYPE *num1,POINT_TYPE *num2);
void Sub64(POINT_TYPE *num1,POINT_TYPE *num2);
void Copy64(POINT_TYPE *from,POINT_TYPE *to);
void ConvSigToDig(DIG48 *sig_data, POINT_TYPE *dig_data);
void Compress_Diff2(U_INT8 signal);
void ConvCompressTo64(INT16 *compr_data, POINT_TYPE *diff2_data);
void Uncompress_Diff2(U_INT8 signal);
void InitCompression(U_INT8 signal);
void ResetCompress(U_INT8 signal);
void ResetUncompress(U_INT8 signal);
void ResetBufIfSingleMode(void);


#ifndef mccabe
/* put initialization in boot section */
#pragma SECTION PROG=boot
#endif
void  InitSignal_1_DigitalPath(void);
void  InitSignal_2_DigitalPath(void);
void  InitSignal_P_DigitalPath(void);
#ifndef mccabe
#pragma SECTION UNDO
#endif



#endif
